<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="校区" prop="campusId">
        <el-select @change="changeCampus" v-model="queryParams.campusId" placeholder="请选择校区" filterable clearable>
          <el-option v-for="(v, i) in campus_list" :key="i" :label="v.name" :value="v.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="园区" prop="parkId">
        <el-select @change="changePark" v-model="queryParams.parkId" placeholder="请先选择校区再选择园区" filterable clearable>
          <el-option v-for="(v, i) in park_list" :key="i" :label="v.name" :value="v.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋" prop="buildingId">
        <el-select @change="changeBuilding" v-model="queryParams.buildingId" placeholder="请先选择园区再选择楼栋" filterable
          clearable>
          <el-option v-for="(v, i) in building_list" :key="i" :label="v.name" :value="v.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="层" prop="floorId">
        <el-select v-model="queryParams.floorId" @change="changeFloor" clearable>
          <el-option v-for="(v, i) in floor_list" :key="i" :label="v.floor" :value="v.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="宿舍" prop="dormitoryId">
        <el-select v-model="queryParams.dormitoryId" clearable filterable>
          <el-option v-for="(v, i) in room_list" :key="i" :label="v.roomNo" :value="v.id"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="学院" prop="deptId">
        <el-select @change="changeDept" v-model="queryParams.deptId" placeholder="请选择学院" filterable clearable>
          <el-option v-for="(v, i) in deptList" :key="i" :label="v.label" :value="v.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="专业" prop="majorId">
        <el-select @change="changeMajor" v-model="queryParams.majorId" placeholder="请先选择学院再选择专业" filterable clearable>
          <el-option v-for="(v, i) in majorList" :key="i" :label="v.majorName" :value="v.majorId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请先选择专业再选择班级" filterable clearable>
          <el-option v-for="(v, i) in classList" :key="i" :label="v.className" :value="v.classId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年级" prop="gradeId">
        <el-select v-model="queryParams.gradeId" placeholder="请选择年级" filterable clearable>
          <el-option v-for="(v, i) in gradeList" :key="i" :label="v.gradeName" :value="v.gradeId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="学号" prop="stuNo">
        <el-input v-model="queryParams.stuNo" placeholder="请输入学生学号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="stuName">
        <el-input v-model="queryParams.stuName" placeholder="请输入学生姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="辅导员姓名" prop="teacherName">
        <el-input v-model="queryParams.teacherName" placeholder="请输入辅导员姓名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label=" ">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="exportVClick">导出</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
          @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-refresh" size="mini" @click="cleanNotStu">释放未在校学生</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" align="center" prop="id" width="60" />
      <el-table-column label="学号" align="center" prop="stuNo" width="100" />
      <el-table-column label="姓名" align="center" prop="stuName" width="100" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="校区" align="center" prop="campusName" width="60" />
      <el-table-column label="园区" align="center" prop="parkName" width="60" />
      <el-table-column label="楼栋" align="center" prop="buildingName" width="60" />
      <el-table-column label="层" align="center" prop="floorName" width="60" />
      <el-table-column label="房间号" align="center" prop="roomNo" width="60" />
      <el-table-column label="入住时间" align="center" prop="checkinTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkinTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改宿舍学生关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="校区" prop="campusId">
          <el-select @change="changeFastCampus" v-model="fastForm.campusId" placeholder="请选择校区" filterable clearable>
            <el-option v-for="(v, i) in campus_list" :key="i" :label="v.name" :value="v.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="园区" prop="parkId">
          <el-select @change="changeFastPark" v-model="fastForm.parkId" placeholder="请先选择校区再选择园区" filterable clearable>
            <el-option v-for="(v, i) in fast_park_list" :key="i" :label="v.name" :value="v.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋" prop="buildingId">
          <el-select @change="changeFastBuilding" v-model="fastForm.buildingId" placeholder="请先选择园区再选择楼栋" filterable
            clearable>
            <el-option v-for="(v, i) in fast_building_list" :key="i" :label="v.name" :value="v.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="层" prop="floorId">
          <el-select v-model="fastForm.floorId" @change="changeFastFloor" clearable>
            <el-option v-for="(v, i) in fast_floor_list" :key="i" :label="v.floor" :value="v.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍" prop="dormitoryId">
          <el-select v-model="form.dormitoryId" clearable>
            <el-option v-for="(v, i) in fast_room_list" :key="i"
              :label="v.roomNo + ' -- ' + v.gender + ' -- 床位 ' + v.bedNum + ' -- 已住 ' + v.occupancy"
              :disabled="v.bedNum <= v.occupancy" :value="v.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生学号" prop="stuNo">
          <el-input v-model="form.stuNo" readonly placeholder="请输入学生学号" />
        </el-form-item>
        <el-form-item label="是否为宿舍长" prop="isDormitoryHead">
          <el-select v-model="form.isDormitoryHead">
            <el-option :label="'否'" :value="0"></el-option>
            <el-option :label="'是'" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入住时间" prop="checkinTime">
          <el-date-picker clearable v-model="form.checkinTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择入住时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent } from "@/api/dormitory/new/stuDom";
import { cleanNotStu } from "@/api/dormitory/basedata/dormitory";
import { fullLoading, groupBy, getChineseKey, getImg, isEmpty } from "@/api/helpFunc";


import {
  listAllCampus, listFloorByBuilding,
  listParkByCampus, listBuildingByPark, listAllRoomByFloor
} from "@/api/dormitory/basedata/dormitory";

import { getDeptName } from "@/api/system/dept";
import { listMajors } from "@/api/stuCQS/basedata/majors";
import { listGrade } from "@/api/stuCQS/basedata/grade";
import { listClass } from "@/api/stuCQS/basedata/class";

export default {
  name: "Student",
  dicts: ['dorm_in_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 宿舍学生关联表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dormitoryId: null,
        bedId: null,
        stuNo: null,
        isDormitoryHead: null,
        status: null,
        checkinTime: null,
        inStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dormitoryId: [
          { required: true, message: "宿舍ID不能为空", trigger: "blur" }
        ],
        bedId: [
          { required: true, message: "床位ID不能为空", trigger: "blur" }
        ],
        stuNo: [
          { required: true, message: "学生学号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态(0未缴费不能为空", trigger: "change" }
        ],
        checkinTime: [
          { required: true, message: "入住时间不能为空", trigger: "blur" }
        ],
      },

      fastForm: {
        campusId: null,
        parkId: null,
        buildingId: null,
        floorId: null,
      },


      deptList: [],//学院列表
      majorList: [],//专业列表
      classList: [],//班级列表
      gradeList: [],//年级列表

      park_list: [],
      building_list: [],
      floor_list: [],
      room_list: [],


      exportV: false,
      campus_list: [],
      fast_park_list: [],
      fast_building_list: [],
      fast_floor_list: [],
      fast_class_list: [],
      fast_major_list: [],
      fast_room_list: []
    };
  },
  created() {
    this.listDept();
    this.listGrade();
    this.getList();
    this.listAllCampus();
  },
  methods: {

    exportVClick() {
      this.download(
        "/dormitory/srs-dormitory-student/export", {
        ...this.queryParams,
      },
        `学生宿舍信息_${new Date().getTime()}.xlsx`
      );
    },

    async changeFloor(val) {
      this.queryParams.dormitoryId = null;

      this.room_list = [];
      if (!isEmpty(val)) {
        let res = await listAllRoomByFloor(this.queryParams.floorId);
        if (res.code == 200) {
          this.room_list = [...res.data];
        }
      }
    },

    async changeBuilding(val) {
      this.queryParams.floorId = null;
      this.floor_list = [];
      this.room_list = [];
      this.queryParams.dormitoryId = null;
      if (!isEmpty(val)) {
        let res = await listFloorByBuilding(this.queryParams.buildingId);
        if (res.code == 200) {
          this.floor_list = [...res.data];
        }
      }
    },
    async changePark(val) {
      this.queryParams.buildingId = null;
      this.queryParams.floorId = null;
      this.queryParams.dormitoryId = null;

      this.building_list = [];
      this.floor_list = [];
      this.room_list = [];

      if (!isEmpty(val)) {
        let res = await listBuildingByPark(this.queryParams.parkId);
        if (res.code == 200) {
          this.building_list = [...res.data];
        }
      }
    },

    async changeCampus(val) {
      this.queryParams.parkId = null;
      this.queryParams.buildingId = null;
      this.queryParams.floorId = null;
      this.queryParams.dormitoryId = null;

      this.park_list = [];
      this.building_list = [];
      this.floor_list = [];
      this.room_list = [];

      if (!isEmpty(val)) {
        let res = await listParkByCampus(this.queryParams.campusId);
        if (res.code == 200) {
          this.park_list = [...res.data];
        }
      }
    },



    async listGrade() {
      let res = await listGrade();
      this.gradeList = [...res.rows];
    },

    async changeMajor() {
      if (!isEmpty(this.queryParams.majorId)) {
        let sdata = {
          pageNum: 1,
          pageSize: 100,
          majorId: this.queryParams.majorId
        }
        let res = await listClass(sdata);
        this.classList = [...res.rows];
      } else {
        this.queryParams.classId = null;
        this.classList = [];
      }

    },
    async changeDept() {
      if (!isEmpty(this.queryParams.deptId)) {
        let sdata = {
          collegeId: this.queryParams.deptId,
          pageNum: 1,
          pageSize: 100
        }
        let res = await listMajors(sdata);
        this.majorList = [...res.rows];
      } else {
        this.queryParams.majorId = null;
        this.majorList = [];
      }
    },
    async listDept() {
      let res = await getDeptName();
      this.deptList = [...res.data];
    },


    async listAllCampus() {
      let res = await listAllCampus();
      if (res.code == 200) {
        this.campus_list = [...res.data];
      }
    },

    async changeFastFloor(val) {
      this.form.dormitoryId = null;

      this.fast_room_list = [];
      if (!isEmpty(val)) {
        let res = await listAllRoomByFloor(this.fastForm.floorId);
        if (res.code == 200) {
          this.fast_room_list = [...res.data];
        }
      }
    },

    async changeFastBuilding(val) {
      this.fastForm.floorId = null;
      this.fast_floor_list = [];
      this.fast_room_list = [];
      this.form.dormitoryId = null;
      if (!isEmpty(val)) {
        let res = await listFloorByBuilding(this.fastForm.buildingId);
        if (res.code == 200) {
          this.fast_floor_list = [...res.data];
        }
      }
    },
    async changeFastPark(val) {
      this.fastForm.buildingId = null;
      this.fastForm.floorId = null;
      this.form.dormitoryId = null;

      this.fast_building_list = [];
      this.fast_floor_list = [];
      this.fast_room_list = [];

      if (!isEmpty(val)) {
        let res = await listBuildingByPark(this.fastForm.parkId);
        if (res.code == 200) {
          this.fast_building_list = [...res.data];
        }
      }
    },

    async changeFastCampus(val) {
      this.fastForm.parkId = null;
      this.fastForm.buildingId = null;
      this.fastForm.floorId = null;
      this.form.dormitoryId = null;

      this.fast_park_list = [];
      this.fast_building_list = [];
      this.fast_floor_list = [];
      this.fast_room_list = [];

      if (!isEmpty(val)) {
        let res = await listParkByCampus(this.fastForm.campusId);
        if (res.code == 200) {
          this.fast_park_list = [...res.data];
        }
      }
    },

    async cleanNotStu() {
      let loading = fullLoading(this);
      let res = await cleanNotStu();
      loading.close();
      if (res.code == 200) {
        this.$message.success(res.msg);
        this.getList();
      }
    },

    /** 查询宿舍学生关联列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dormitoryId: null,
        bedId: null,
        stuNo: null,
        isDormitoryHead: null,
        status: null,
        checkinTime: null,
        inStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.fastForm.campusId = null;
      this.fastForm.parkId = null;
      this.fastForm.buildingId = null;
      this.fastForm.floorId = null;
      this.form.dormitoryId = null;

      this.fast_park_list = [];
      this.fast_building_list = [];
      this.fast_floor_list = [];
      this.fast_room_list = [];

      this.reset();
      this.open = true;
      this.title = "添加宿舍学生关联";
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      let loading = fullLoading(this);

      const id = row.id || this.ids;
      this.fastForm.campusId = row.campusId;
      this.fastForm.parkId = row.parkId;
      this.fastForm.buildingId = row.buildingId;
      this.fastForm.floorId = row.floorId;

      let res1 = await listParkByCampus(row.campusId);
      if (res1.code == 200) {
        this.fast_park_list = [...res1.data];
      }

      let res2 = await listBuildingByPark(row.parkId);
      if (res2.code == 200) {
        this.fast_building_list = [...res2.data];
      }

      let res3 = await listFloorByBuilding(row.buildingId);
      if (res3.code == 200) {
        this.fast_floor_list = [...res3.data];
      }

      let res4 = await listAllRoomByFloor(row.floorId);
      if (res4.code == 200) {
        this.fast_room_list = [...res4.data];
      }

      loading.close();
      this.reset();
      getStudent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改宿舍学生关联";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                //this.open = false;
                this.getList();
              }

            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除宿舍学生关联编号为"' + ids + '"的数据项？').then(function () {
        return delStudent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dormitory/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
